package com.lzp.controller;

import com.lzp.pojo.Users;
import com.lzp.pojo.vo.UsersVO;
import com.lzp.utils.RedisOperator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.UUID;

@RestController
public class BaseController {

    @Autowired
    private RedisOperator redis;

    /**
     * 默认分页条数
     */
    public static final Integer COMMON_PAGE_SIZE = 5;

    /**
     * 搜索分页条数
     */
    public static final Integer SEARCH_PAGE_SIZE = 20;

    /**
     * 用户cookie名
     */
    public static final String PASSPORT_COOKIE = "user";

    /**
     * 购物车cookie名
     */
    public static final String SHOPCART_COOKIE = "shopcart";

    public static final String REDIS_USER_TOKEN = "redis_user_token";

    /**
     * 微信支付成功 -- 支付中心 -- 平台
     */
    public static final String PAY_RETURN_URL
            = "http://192.168.233.130/foodie-api-1.0-SNAPSHOT/orders/notifyMerchantOrderPaid";

    /**
     * 支付中心的调用地址
     */
    public static final String PAYMENT_URL
            = "http://payment.t.mukewang.com/foodie-payment/payment/createMerchantOrder";

    /**
     * 用户图像服务器地址 对于windows上传位置是当前磁盘
     * /workspaces/images/emall/faces
     */
    public static final String IMAGE_USER_FACE_LOCATION = File.separator + "workspaces" +
            File.separator + "images" +
            File.separator + "emall" +
            File.separator + "faces";

    /**
     * 实现用户的Redis会话
     *
     * @param user
     * @return
     */
    public UsersVO convertUsersVO(Users user) {
        String uniqueToken = UUID.randomUUID().toString().trim();
        redis.set(REDIS_USER_TOKEN + ":" + user.getId(), uniqueToken);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(user, usersVO);
        usersVO.setUserUniqueToken(uniqueToken);
        return usersVO;
    }

}
